import mapBuild from "./mapBuild";
import dataHandle from "./dataHandle";
import mapCenter from "./mapCenter";
import faultAlertHandle from "./faultAlertHandle";
// import { useSystemrStore } from "../../stores/system";

// const systemStore = useSystemrStore();
const allLineNotFaultList = [
  {
    line_name: "13号线",
    train_type: "13A01",
    train_no: "1323",
    fault_code: "CCF_68",
    fault_time: "1757309298252",
    fault_system: "CCF",
    fault_name: "ATP紧急制动",
    fault_level: "1",
    fault_cause: null,
    train_place: "Tc2",
    end_time: null,
    fault_alias: "c_p01D1_133_3",
    is_send: "0",
    is_serious: "2",
    current_stationid: "28",
    terminate_stationid: "31",
    solution: null,
    maintenance_tips: null,
    fault_status: null,
    set_serious_time: null,
    set_serious_user: null,
    line_code: null,
    next_stationid: "29",
    put_into_states: "inLine",
    set_serious_reason: null,
    insert_time: "1757309054060",
    fault_type: "normal",
    speed: "56.02",
    run_mode: "ATO模式",
    driver_activator: "Tc2",
    control_mode: "无",
  },
  {
    line_name: "1号线",
    train_type: "01A05",
    train_no: "01051",
    fault_code: "msg2_654",
    fault_time: "1757309291768",
    fault_system: "msg2",
    fault_name: "M2车_3B门严重故障(6门)",
    fault_level: "3",
    fault_cause: null,
    train_place: "-",
    end_time: null,
    fault_alias: "c_p000C_81_3",
    is_send: "0",
    is_serious: "2",
    current_stationid: "0",
    terminate_stationid: "0",
    solution: null,
    maintenance_tips: null,
    fault_status: null,
    set_serious_time: null,
    set_serious_user: null,
    line_code: null,
    next_stationid: "0",
    put_into_states: "inLine",
    set_serious_reason: null,
    insert_time: "1757309054164",
    fault_type: "normal",
    speed: "0.00",
    run_mode: "制动",
    driver_activator: "无",
    control_mode: "主控未激活",
  },
  {
    line_name: "13号线",
    train_type: "13A01",
    train_no: "1323",
    fault_code: "CCF_68",
    fault_time: "1757309290729",
    fault_system: "CCF",
    fault_name: "ATP紧急制动",
    fault_level: "1",
    fault_cause: null,
    train_place: "Tc1",
    end_time: null,
    fault_alias: "c_p01D1_8_3",
    is_send: "0",
    is_serious: "2",
    current_stationid: "28",
    terminate_stationid: "31",
    solution: null,
    maintenance_tips: null,
    fault_status: null,
    set_serious_time: null,
    set_serious_user: null,
    line_code: null,
    next_stationid: "29",
    put_into_states: "inLine",
    set_serious_reason: null,
    insert_time: "1757309047374",
    fault_type: "normal",
    speed: "55.68",
    run_mode: "ATO模式",
    driver_activator: "Tc2",
    control_mode: "无",
  },
];
const mapConfig = (dom: string) => {
  console.log("mapConfig dom", dom);
  const urlStr =
    "http://172.20.62.103/iserver/services/map-china400/rest/maps/China_4326";
  const centerPoint = new (window as any).SuperMap.Geometry.Point(
    121.5305882678,
    31.204674024221
  );
  const level = 1;
  const mapHandle = mapBuild(
    dom,
    urlStr,
    "China",
    centerPoint,
    level,
    initAllLine
  );
  const myDataHandle = dataHandle(mapHandle);
  const eventTrainMap: any = {};
  const myFaultAlertHandle = faultAlertHandle();

  console.log("mapConfig mapBuild", mapHandle);

  /********** 路网页面webSocket处理 *********/
  function webSocketInit(sendData: any, myTrainMove: any) {
    // 清空内容
    // $(".faultBox, .warnBox").empty();
    // // 路网页面，严重、一般、轻微故障和预警的点击事件
    // filterClick(myTrainMove);
    // socketInit(JSON.stringify(sendData), function (evt) {
    //   var webData = JSON.parse(evt.data);
    //   if (webData.msgType == "publish") {
    //     var realData = webData.value;
    //     var trainNo = realData.trainNo;
    //     if (webData.dataCollection == "statistics") {
    //       mapCenter.statisticsFn(webData); //更新各线路列车
    //     } else if (webData.dataCollection == "all" && webData.from == "spark") {
    //       realData.faults = realData.faults.map((item) => {
    //         const fault_key =
    //           item["train_no"] +
    //           "_" +
    //           item["fault_code"] +
    //           "_" +
    //           item["train_place"] +
    //           item["fault_alias"];
    //         if (["1", "2", "3"].includes(item.fault_level)) {
    //           let flag = false;
    //           if (allLineNotFaultList && allLineNotFaultList.length > 0) {
    //             allLineNotFaultList.forEach((el) => {
    //               const fault_key2 =
    //                 el["train_no"] +
    //                 "_" +
    //                 el["fault_code"] +
    //                 "_" +
    //                 el["train_place"] +
    //                 el["fault_alias"];
    //               if (fault_key === fault_key2) {
    //                 flag = true;
    //                 return item;
    //               }
    //             });
    //           }
    //           if (!flag) {
    //             item.fault_level = "0";
    //           }
    //           return item;
    //         }
    //         return item;
    //       });
    //       common.dealAggregationData(realData, webData.from, function () {
    //         //更新故障数据
    //         dealFaultData(trainNo, realData, myTrainMove);
    //       });
    //     } else if (webData.dataCollection == "all" && webData.from == "flink") {
    //       //更新预警数据
    //       setEventLevel(realData, false, myTrainMove);
    //       if (
    //         mapCenter.filterCkeck(eventTrainMap[trainNo], "alert", realData)
    //       ) {
    //         return;
    //       }
    //       myFaultAlertHandle.appendAlertList(realData, ".allLine .warnBox");
    //       myTrainMove.changeIcon(realData);
    //     }
    //   }
    // });
  }
  //更新故障数据
  function dealFaultData(trainNo: string, realData: any, myTrainMove: any) {
    setEventLevel(realData, true, myTrainMove);
    // if (mapCenter.filterCheck(eventTrainMap[trainNo], "fault", realData)) {
    //   return;
    // }
    myTrainMove.changeIcon(realData);
    myTrainMove.refreshWebSocket(realData, "all", null, null);

    // var faultLevel = myTrainMove.getMaxFaultLevel(realData.faults);
    // 为了20191202的演示，超过10个故障不显示
    // if (faultLevel == 3 && Object.keys(myTrainMove.faultTrainMap).length >= myTrainMove.allLineFaultNum) {
    //     if (realData.lineNo == '17') {
    //         // 如果是17号线的故障，移除掉之前的某辆车，补上17号线的
    //         for (let trainNoK in myTrainMove.faultTrainMap) {
    //             if (Number(trainNoK.substring(0, 2)) != 17) {
    //                 myTrainMove.dropTrain(trainNoK)
    //             }
    //         }
    //     } else {
    //         return false;
    //     }
    // }
    // console.log('首页地图页面右边的事件中心')
    // if (notConfirmedState != undefined && notConfirmedState.length > 0) {
    // allLineNotFaultList = notConfirmedState;
    realData["showFaultPage"] = "mapConfig";
    myFaultAlertHandle.appendFaultList(
      realData,
      ".allLine .faultBox",
      allLineNotFaultList,
      "showstaticMonitorFault"
    );
    // }
  }
  function setEventLevel(realData: any, isFault: boolean, dom: any) {
    const trainNo = realData.trainNo;
    let tmpEventInfo = eventTrainMap[trainNo] || {};
    if (isFault) {
      const faultLevel = dom.getMaxFaultLevel(realData.faults);
      tmpEventInfo["fault"] = faultLevel;
      if (faultLevel <= 0) {
        delete tmpEventInfo["fault"];
      }
      eventTrainMap[trainNo] = tmpEventInfo;
    } else {
      const alertLevel = dom.getMaxAlertLevel(realData.alerts);
      tmpEventInfo["alert"] = alertLevel;
      if (alertLevel <= 0) {
        delete tmpEventInfo["alert"];
      }
      eventTrainMap[trainNo] = tmpEventInfo;
    }
  }
  // function filterClick(myTrainMove:any) {
  // var faultFn = function (type, level) {};
  // var warnFn = function (type, level) {
  //   $.each(eventTrainMap, function (item, value) {
  //     if (value[type] == level) {
  //       myTrainMove.dropTrain(item);
  //     }
  //   });
  // };
  // mapCenter.filterClickFn(myFaultAlertHandle, faultFn, warnFn);
  // }
  function initAllLine() {
    // cacheData.getCacheSignal().then((signals) => {
    console.log("initAllLine");
    let sendInfo = { type: "all", signals: {} };
    myDataHandle.start(sendInfo, webSocketInit);
    // });
  }
};
export default mapConfig;
